package com.jiuyou.storeController.store;

import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.HandleVideoSrc;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fitment.domain.StoreFitment;
import com.ruoyi.fitment.service.IStoreFitmentService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author chengliang
 * @date 2022/8/25
 **/
@RestController
@RequestMapping("/store/fitment")
public class StoreFitmentStoreController extends StoreBaseController {
    @Autowired
    private IStoreFitmentService fitmentService;
    @Autowired
    private ISysConfigService configService;

    @RequestMapping("/edit")
    public StoreFitment edit(@RequestBody StoreFitment fitment){
        if(StringUtils.isNotEmpty(fitment.getVideoUrl())){
            Map<String, String> val = configService.getVal();
            fitment.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),fitment.getVideoUrl()));
        }
        if(fitment.getId()!=null){
            fitmentService.updateStoreFitment(fitment);
        }else{
            StoreUserRole storeUserRole = getStoreUserRole();
            fitment.setStoreId(storeUserRole.getStoreId());
            fitment.setFitmentType(DicConstants.FITMENT_TYPE_SINGLE.getCode());
            fitmentService.insertStoreFitment(fitment);
        }
        return fitment;
    }
    @RequestMapping("/getInfo")
    public StoreFitment getInfo(){
        StoreFitment storeFitment = new StoreFitment();
        StoreUserRole storeUserRole = getStoreUserRole();
        storeFitment.setStoreId(storeUserRole.getStoreId());
        storeFitment.setFitmentType(DicConstants.FITMENT_TYPE_SINGLE.getCode());
        List<StoreFitment> storeFitments = fitmentService.selectStoreFitmentList(storeFitment);
        return storeFitments.get(0);
    }

    @RequestMapping("/addImage")
    public AjaxResult addImage(@RequestBody StoreFitment fitment){
        fitment.setMediaType(DicConstants.MEDIA_TYPE_IMG.getCode());
        StoreUserRole storeUserRole = getStoreUserRole();
        fitment.setStoreId(storeUserRole.getStoreId());
        fitment.setFitmentType(DicConstants.FITMENT_TYPE_MULTIPLE.getCode());
        fitmentService.insertStoreFitment(fitment);
        return AjaxResult.success(fitment);
    }
    @RequestMapping("/addVideo")
    public AjaxResult addVideo(@RequestBody StoreFitment fitment){
        fitment.setMediaType(DicConstants.MEDIA_TYPE_VIDEO.getCode());
        StoreUserRole storeUserRole = getStoreUserRole();
        fitment.setStoreId(storeUserRole.getStoreId());
        fitment.setFitmentType(DicConstants.FITMENT_TYPE_MULTIPLE.getCode());
        Map<String, String> val = configService.getVal();
        fitment.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),fitment.getVideoUrl()));
        fitmentService.insertStoreFitment(fitment);
        return AjaxResult.success(fitment);
    }
    @RequestMapping("/del/{id}")
    public AjaxResult del(@PathVariable Long id){
        fitmentService.deleteStoreFitmentById(id);
        return AjaxResult.success();
    }
    @RequestMapping("/getList/{type}")
    public TableDataInfo getList(@PathVariable String type){

        StoreFitment fitment = new StoreFitment();
        fitment.setMediaType(type);
        StoreUserRole storeUserRole = getStoreUserRole();
        fitment.setStoreId(storeUserRole.getStoreId());
        fitment.setFitmentType(DicConstants.FITMENT_TYPE_MULTIPLE.getCode());
        List<StoreFitment> storeFitments = fitmentService.selectStoreFitmentList(fitment);
        return getDataTable(storeFitments);
    }
}
